package com.zzptc.zzj.a1124;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.zzj.a1124.entity.UserFactory;
import com.zzptc.zzj.a1124.entity.UserIpdz;
import com.zzptc.zzj.a1124.service.UserFactoryService;
import com.zzptc.zzj.a1124.service.UserIpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getUserFactory();
        getIp();
    }

    private void getIp() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/ip/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserIpService userIpService = retrofit.create(UserIpService.class);

        Call<UserIpdz> ipdz = userIpService.getIpdz(0);

        ipdz.enqueue(new Callback<UserIpdz>() {
            @Override
            public void onResponse(Call<UserIpdz> call, Response<UserIpdz> response) {
                UserIpdz body = response.body();
                Log.d(TAG, "onResponse: "+body.getResult().getCity());
            }

            @Override
            public void onFailure(Call<UserIpdz> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }



    private void getUserFactory() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //
        UserFactoryService userFactoryService = retrofit.create(UserFactoryService.class);

        Call<UserFactory> info = userFactoryService.getInfo(2);


        info.enqueue(new Callback<UserFactory>() {
            @Override
            public void onResponse(Call<UserFactory> call, Response<UserFactory> response) {
                UserFactory body = response.body();
                Log.d(TAG, "onResponse: "+body.getData().get(0).getNickName());//logd
            }

            @Override
            public void onFailure(Call<UserFactory> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }//访问不了网android:usesCleartextTraffic="true"

        });


    }
}
